package com.example.smartstart.controller;

import com.example.smartstart.entity.Point;
import com.example.smartstart.model.MyResponseData;
import com.example.smartstart.model.Result;
import com.example.smartstart.service.IPointService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dlx
 * @since 2024-07-22
 */
@RestController
@RequestMapping("point/")
public class PointController {

    @Autowired
    private IPointService iPointService;

    // 消耗或获得积分
    @PatchMapping("awardOrSpendPoint")
    public MyResponseData awardOrSpendPoint(@RequestBody Point point){
        MyResponseData data = new MyResponseData();
        int result = iPointService.awardOrSpendPoint(point);
        if (result>=0){
            data.setMessage("操作成功");
            data.setErrorCode(0);
            data.setArg1(result);
        }else {
            data.setMessage("积分不足");
            data.setErrorCode(1);
        }
        return data;
    }

    @GetMapping("getUserPoints")
    public Result getUserPoints(Integer userId){
        return Result.success(iPointService.getById(userId));
    }

}
